<?php
require_once ('../config/path_config.php');
require(ROOTDIR."admin/admin-header.php");
require_once(ROOTDIR."lang/cn.php");
require_once(ROOTDIR."include/set_get_key.php");
require_once (ROOTDIR."common/db_utils.php");

// 1. 允许管理员查看题目列表
$isAllowAccess = isset($_SESSION['administrator']);
$isAllowAccess |= isset($_SESSION['problem_editor']);
if (!$isAllowAccess ) {
    echo "<br/>当前账号不具有相关权限，请换用其他账号登陆<br/>";
    echo "<a href='../loginpage.php'>Please Login First!</a>";
    exit(1);
}

$keyword="";
if(isset($_GET['keyword'])) {
    $keyword=$_GET['keyword'];
}

$defunct="";
if(isset($_GET['defunct'])) {
    $defunct = $_GET['defunct'];
    if ( strcmp($defunct, "all") == 0 ) {
        $defunct="";
    }
}

$sql="SELECT max(`problem_id`) as upid FROM `problem`";
$page_cnt=100;
$result=DbUtil::getInstance()->query($sql);
$row=$result[0];
$cnt=intval($row['upid'])-1000;
$cnt=intval($cnt/$page_cnt)+(($cnt%$page_cnt)>0?1:0);
if (isset($_GET['page'])){
        $page=intval($_GET['page']);
}else $page=$cnt;
$pstart=1000+$page_cnt*intval($page-1);
$pend=$pstart+$page_cnt;
echo "<br/><div class='container'>";
echo "<form action=problem_list.php>";
echo "<select class='input-mini' onchange=\"location.href='problem_list.php?page='+this.value;\">";
for ($i=1;$i<=$cnt;$i++){
        if ($i>1) echo '&nbsp;';
        if ($i==$page) echo "<option value='$i' selected>";
        else  echo "<option value='$i'>";
        echo $i+9;
        echo "**</option>";
}
echo "</select>";

$sql="";
if( $keyword || $defunct ) {
    $sql = "select `problem_id`,`title`,`accepted`,`source`,`defunct`, `creater_id` FROM `problem` ";
    $sqlWhere = null;
    $args=array();
    if ( $keyword ) {
        $keywordstr="%$keyword%";
        $sqlWhere = "where title like ? or source like ? or creater_id=? ";
        array_push($args, $keywordstr, $keywordstr, $keyword);
    }

    if ( $defunct ) {
        if ( $sqlWhere ) {
            $sqlWhere .= " and defunct = ?";
            array_push($args, $defunct);
        }
        else {
            $sqlWhere = " where defunct = ?";
            array_push($args, $defunct);
        }
    }
    $sql .= $sqlWhere;
    $result = DbUtil::getInstance()->query2($sql, $args);
}
else{
	$sql="select `problem_id`,`title`,`accepted`,`source`,`defunct`, `creater_id` FROM `problem`";
    $sql.=" where problem_id>=? and problem_id<=?";
    $result = DbUtil::getInstance()->query($sql, $pstart, $pend);
}

// 已启用的题目数
$sqlTotProblem = "select count(*) from problem";
$resultTotProblem = DbUtil::getInstance()->query($sqlTotProblem);
$total_problem_cnt = 0;
if (count($resultTotProblem) > 0 ) {
    $total_problem_cnt = intval($resultTotProblem[0][0]);
}

// 题目总数
$sqlValidProblem = "select count(*) from problem  where defunct='N'";
$resultValidProblem = DbUtil::getInstance()->query($sqlValidProblem);
$valid_problem_cnt = 0;
if (count($resultValidProblem) > 0 ) {
    $valid_problem_cnt = intval($resultValidProblem[0][0]);
}

?>

<form action=problem_list.php>
    <input name=keyword>
    <select name='defunct'>
        <option value="N">Avaliable</option>
        <option value="Y">Reserved</option>
        <option selected value="all">All</option>
    </select>
    <input type=submit value="<?php echo $MSG_SEARCH?>" >
</form>
<span style="color:blue;">已启用/总数:&nbsp;&nbsp;<?php echo $valid_problem_cnt?>/<?php echo $total_problem_cnt?></span>
<?php
echo "<center><table class='table table-striped' width=90% border=1>";
echo "<form method=post action=contest_add.php>";
// modify by swb 2020.05.25 删除比赛创建部分
//echo "<tr><td colspan=8><input type=submit name='problem2contest' value='CheckToNewContest'>";
echo "<tr><td colspan=8>";
echo "<tr><td>题目ID<td>标题<td>AC<td>所属类别<td>创建人";

if(isset($_SESSION['administrator'])||isset($_SESSION['problem_editor'])){
    echo "<td>状态<td>删除<td>编辑</tr>";
}
foreach($result as $row) {
    echo "<tr>";
    echo "<td>".$row['problem_id'];
    echo "<input type=checkbox name='pid[]' value='".$row['problem_id']."'>";
    echo "<td><a href='../problem.php?id=".$row['problem_id']."'>".$row['title']."</a>";
    echo "<td>".$row['accepted'];
    echo "<td>".$row['source'];
    echo "<td>".$row['creater_id'];
    $allowEdit = isset($_SESSION['administrator']);
    if ( !$allowEdit ) {
        // 题目编辑者可编辑题目，和修改题目的测试数据
        $allowEdit = isset($_SESSION['problem_editor']) && strtolower($row['creater_id']) == strtolower($_SESSION['user_id']);
    }
    if ( $allowEdit ) {
        echo "<td><a href=problem_df_change.php?id="
            . $row['problem_id'] . "&getkey=" . $_SESSION['getkey'] . ">"
            . ($row['defunct'] == "N" ? "<span titlc='click to reserve it' class=green>Available</span>"
                : "<span class=red title='click to be available'>Reserved</span>")
            . "</a><td>";
    }else {
        echo "<td><a>"
                . ($row['defunct'] == "N" ? "<span titlc='click to reserve it' class=green>Available</span>"
                : "<span class=red title='click to be available'>Reserved</span>")
            . "</a><td>";
    }

    if( $allowEdit ) {
        if( function_exists("system") ) {
            ?>
            <a href=# onclick='javascript:if(confirm("Delete <?php echo $row['problem_id']?>?"))
                location.href="problem_del.php?id=<?php echo $row['problem_id']?>&getkey=<?php echo $_SESSION['getkey']?>";'>
                Delete</a>
            <?php
        }
    }

    if( $allowEdit ) {
        echo "<td><a href=problem_edit.php?id=".$row['problem_id']."&getkey=".$_SESSION['getkey'].">Edit</a>";
        //echo "<td><a href='javascript:phpfm(".$row['problem_id'].");'>TestData</a>";
    }
    echo "</tr>";
}

// echo "<tr><td colspan=8><input type=submit name='problem2contest' value='CheckToNewContest'>";
echo "<tr><td colspan=8>";
echo "</tr></form>";
echo "</table></center>";
?>
<script src='../template/bs3/jquery.min.js' ></script>
<script>
function phpfm(pid){
        //alert(pid);
        $.post("phpfm.php",{'frame':3,'pid':pid,'pass':''},function(data,status){
                if(status=="success"){
                        document.location.href="phpfm.php?frame=3&pid="+pid;
                }
        });
}
</script>
</div>
